import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(LoginState state, Dispatch dispatch, ViewService viewService) {
  // dispatch(LoginActionCreator.reloadLoggingInfo());
  var isCowndown = state.currCountTime > 0;
  return Scaffold(
    appBar: AppBar(
      backgroundColor: Colors.blue,
      title: Text('华安易邻里-物管'),
    ),
    body: GestureDetector(
      onTap: () {
        //隐藏输入法
        FocusScope.of(viewService.context).requestFocus(new FocusNode());
      },
      child: Container(
        color: Colors.white,
        padding: EdgeInsets.fromLTRB(15, 50, 15, 10),
        child: ListView(
          children: <Widget>[
            Stack(
              alignment: AlignmentDirectional.centerEnd,
              children: <Widget>[
                TextField(
                  controller: state.textTelephoneView,
                  onChanged: (msg) {
                    dispatch(LoginActionCreator.showOrHideResetButton());
                  },
                  decoration: InputDecoration(
                    hintText: '请输入账号',
                    labelText: '手机号',
                  ),
                  maxLines: 1,
                  maxLength: 11,
                  keyboardType: TextInputType.phone,
                ),
                Offstage(
                  offstage: state.showClearButton,
                  child: IconButton(
                    icon: Icon(Icons.clear),
                    onPressed: () {
                      dispatch(LoginActionCreator.onClearTelphone());
                    },
                  ),
                ),
              ],
            ),
            Row(
              children: <Widget>[
                Expanded(
                  child: TextField(
                    controller: state.textVerifyCodeView,
                    obscureText: false, //设置显示密码
                    decoration: InputDecoration(
                      hintText: '输入密码',
                      labelText: '密码',
                    ),
                    maxLength: 12,
                    maxLines: 1,
                    keyboardType: TextInputType.number,
                  ),
                ),
                Offstage(
                  offstage: true,
                  child: RaisedButton(
                    child: Text(!isCowndown
                        ? '发送验证码'
                        : '发送验证码(${state.currCountTime})'),
                    onPressed: () {
                      if (!isCowndown)
                        dispatch(LoginActionCreator.onSendSmsCode());
                    },
                  ),
                ),
              ],
            ),
            RaisedButton(
              color: Colors.blue,
              child: Text(
                '登录',
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
              onPressed: () {
                dispatch(LoginActionCreator.onLogin());
              },
            ),
            Text(
              '账号由服务商提供，不支持自主注册',
              style: TextStyle(fontSize: 12, color: Colors.grey),
            ),
            // Expanded(
            //       child: Text('本应用不提供账号注册，由服务商免费提供'),
            //     ),
          ],
        ),
      ),
    ),
  );
}
